package cn.txauction.web.struts.plugin;

import java.util.Date;

import javax.servlet.ServletException;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.txauction.business.service.ICommonService;
import cn.txauction.common.AppDicts;
import cn.txauction.common.AppSetting;
import cn.txauction.common.Constants;
import cn.txauction.web.struts.converter.DateConverter;

public class AppInitialPlugIn implements PlugIn {

	public void destroy() {
	}

	public void init(ActionServlet servlet, ModuleConfig config)
			throws ServletException {
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servlet.getServletContext());
		ICommonService service = (ICommonService)ctx.getBean("commonService");
		AppDicts dicts = new AppDicts();
		dicts.setDicts(service.getAppDicts());		
		
		servlet.getServletContext().setAttribute(Constants.ApplicationKeys.DICTS, dicts);
		
		//other initial
		ConvertUtils.register(new DateConverter(), Date.class);
		
		AppSetting.pagesize = Integer.parseInt(servlet.getServletContext().getInitParameter("pageSize"));
		AppSetting.pageBarSize = Integer.parseInt(servlet.getServletContext().getInitParameter("pageBarSize"));
	}

}
